<CFSET varPATTERNCODE="">
<CFSET varPATTERNCODEDesc="">
<CFSET varSchedType="">
<CFSET effDate = "">
<CFSET sCateg = "">
<cfparam name="sFrom" default="#Now()#">
<cfset sFrom = CreateODBCDate(sFrom)>
<CFIF sFolder EQ "daterange">
	<!---POOL RECORDS--->
	<CFQUERY NAME="getPoolSubSked" DATASOURCE="#client.company_dsn#">
		SELECT * FROM CINWSSUBSKEDPOOL
		WHERE PERSONNELIDNO='#appno#'
			AND REFERENCEDATE = <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
			AND WSNUMBER_FK = '#wsIDnum#'
			AND PATTERNCODE != ''
	</CFQUERY>
	<CFIF getPoolSubSked.RECORDCOUNT EQ 0>
		<CFQUERY NAME="getPoolTemp" DATASOURCE="#client.company_dsn#">
			SELECT * FROM CINWSTEMPSKEDPOOL
			WHERE PERSONNELIDNO='#appno#'
				AND STARTDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
        		AND ENDDATE >=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
				AND WSNUMBER_FK = '#wsIDnum#'
		</CFQUERY>
		<CFSET varPoolCODE=getPoolTemp.WORKSKEDCODE>
		<CFSET varPoolType="Temporary">
        <cfset effDate = getPoolTemp.STARTDATE>
        <CFSET sCateg = getPoolTemp.CATEGORY>

		<CFIF getPoolTemp.RECORDCOUNT EQ 0>
			<CFQUERY NAME="getPoolMaster" DATASOURCE="#client.company_dsn#">
				SELECT * FROM CINWSMASTERPOOL
				WHERE PERSONNELIDNO='#appno#'
					AND ACTIVESKED='Y'
					AND STARTDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
					AND WSNUMBER_FK = '#wsIDnum#'
			</CFQUERY>
			<CFIF getPoolMaster.RECORDCOUNT>
				<CFSET varPoolCODE=getPoolMaster.WORKSKEDCODE>	
				<CFSET varPoolType="Permanent">
                <cfset effDate = getPoolMaster.STARTDATE>
                <CFSET sCateg = getPoolMaster.CATEGORY>
			<CFELSE>
				<CFSET varPoolCODE="">	
				<CFSET varPoolType="">
			</CFIF>
		</CFIF>
		<!------>
        
	<CFELSE>
		<CFSET varPoolCODE=getPoolSubSked.PATTERNCODE>
		<CFSET varPoolType="Sub-Temporary">
	</CFIF>
	<CFSET sDAYTYPEPool="">
	<CFIF varPoolType NEQ "Sub-Temporary">
		<CFSET sDAY=DayOfWeekAsString(DayOfWeek(sFrom))>
        <CFQUERY NAME="getpoolDTL" DATASOURCE="#client.company_dsn#">
            SELECT *
            FROM CLKWORKSKED
            WHERE WORKSKEDCODE='#varPoolCODE#'
        </CFQUERY>
		<CFSET sDAYTYPEPool=EVALUATE("getpoolDTL." & sDAY)>
        <CFSET sCATEGORY = getpoolDTL.CATEGORY>
		<cfif  LCASE(getpoolDTL.CATEGORY) eq 'ALT'>
            <CFSET SkedCode = getpoolDTL.WORKSKEDCODE>
            <cfinclude template="_getAlt.cfm">
            <CFSET sDAYTYPE = getlkDTL.DAYTYPE>
        </cfif>

	<CFELSE>
		<CFQUERY NAME="getpoolDTL" DATASOURCE="#client.company_dsn#">
			SELECT PATTERNCODE,DAYTYPE,TIMEOUT,CATEGORY,TIMEIN
			FROM CLKWSPATTERN
			WHERE PATTERNCODE='#varPoolCODE#'
		</CFQUERY>
		<CFSET sDAYTYPE=getpoolDTL.DAYTYPE>
        <CFSET sCATEGORY = getpoolDTL.CATEGORY>
	</CFIF>
	
	<!---CURRENT RECORDS--->
	<CFQUERY NAME="getCurrentSubSked" DATASOURCE="#client.company_dsn#">
		SELECT * FROM CINWSSUBSKED
		WHERE PERSONNELIDNO='#appno#'
			AND REFERENCEDATE = <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">

	</CFQUERY>
	<CFIF getCurrentSubSked.RECORDCOUNT EQ 0>
		<CFQUERY NAME="getCurrentTemp" DATASOURCE="#client.company_dsn#">
			SELECT * FROM CINWSTEMPSKED
			WHERE PERSONNELIDNO='#appno#'
             AND STARTDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
		     AND ENDDATE >=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
		</CFQUERY>
		<CFSET varPATTERNCODE=getCurrentTemp.WORKSKEDCODE>
		<CFSET varSchedType="Temporary">
        <cfset effDate = getCurrentTemp.STARTDATE>
        <CFSET sCateg = getCurrentTemp.CATEGORY>
		<CFIF getCurrentTemp.RECORDCOUNT EQ 0>
			<CFQUERY NAME="getCurrentMaster" DATASOURCE="#client.company_dsn#">
				SELECT * FROM CINWSMASTER
				WHERE PERSONNELIDNO='#appno#'
					AND ACTIVESKED='Y'
					AND STARTDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
			</CFQUERY>
			
			<CFIF getCurrentMaster.RECORDCOUNT>
				<CFSET varPATTERNCODE=getCurrentMaster.WORKSKEDCODE>	
				<CFSET varSchedType="Permanent">
                <cfset effDate = getCurrentMaster.STARTDATE>
                <CFSET sCateg = getCurrentMaster.CATEGORY>
			<CFELSE>
				<CFSET varPATTERNCODE="">	
				<CFSET varSchedType="">
                 <cfset effDate = "">
			</CFIF>
		</CFIF>
		<!------>
	<CFELSE>
		<CFSET varPATTERNCODE=getCurrentSubSked.PATTERNCODE>
		<CFSET varSchedType="Sub-Temporary">
	</CFIF>
	
	<CFSET sDAYTYPE="">
	<CFIF varSchedType NEQ "Sub-Temporary">
    	
			<CFSET sDAY=DayOfWeekAsString(DayOfWeek(sFrom))>
            <CFQUERY NAME="getlkDTL" DATASOURCE="#client.company_dsn#">
                SELECT *
                FROM CLKWORKSKED
                WHERE WORKSKEDCODE='#varPATTERNCODE#'
            </CFQUERY>
            <CFSET sDAYTYPE=EVALUATE("getlkDTL." & sDAY)>
            <CFSET sCATEGORY = getlkDTL.CATEGORY>
            <cfif  LCASE(getlkDTL.CATEGORY) eq 'ALT'>
				<CFSET SkedCode = getlkDTL.WORKSKEDCODE>
                <cfinclude template="_getAlt.cfm">
                <CFSET sDAYTYPE = getlkDTL.DAYTYPE>
        	</cfif>

	<CFELSE>		
    	<CFQUERY NAME="getlkDTL" DATASOURCE="#client.company_dsn#">
			SELECT PATTERNCODE,DAYTYPE,TIMEOUT,CATEGORY,TIMEIN
			FROM CLKWSPATTERN
			WHERE PATTERNCODE='#varPATTERNCODE#'		
         </CFQUERY>
         
		<CFSET sDAYTYPE=getlkDTL.DAYTYPE>
        <CFSET sCATEGORY = getlkDTL.CATEGORY>
	</CFIF>
    

</CFIF>

<CFIF sFolder EQ "permsched">
	<CFQUERY NAME="getNewMaster" DATASOURCE="#client.company_dsn#">
		SELECT * FROM CINWSMASTERPOOL
		WHERE WSNUMBER_FK = '#wsIDnum#'
			AND PERSONNELIDNO='#appno#'
		ORDER BY STARTDATE
	</CFQUERY>
	<CFSET varPATTERNCODE=getNewMaster.WORKSKEDCODE>
</CFIF>

<CFIF sFolder EQ "tempsched">
	<CFQUERY NAME="getNewMaster" DATASOURCE="#client.company_dsn#">
		SELECT * FROM CINWSTEMPSKEDPOOL
		WHERE WSNUMBER_FK = '#wsIDnum#'
			AND PERSONNELIDNO='#appno#'
		ORDER BY STARTDATE
	</CFQUERY>
	<CFSET varPATTERNCODE=getNewMaster.WORKSKEDCODE>
</CFIF>

<CFIF sFolder EQ "subsched">
	<CFQUERY NAME="getNewMaster" DATASOURCE="#client.company_dsn#">
		SELECT * FROM CINWSSUBSKEDPOOL
		WHERE WSNUMBER_FK = '#wsIDnum#'
			AND PERSONNELIDNO='#appno#'
			AND PATTERNCODE!='' AND PATTERNCODE IS NOT NULL
		ORDER BY REFERENCEDATE
	</CFQUERY>
	<CFSET varPATTERNCODE=getNewMaster.PATTERNCODE>
</CFIF>

<cfquery name="getLVDtls" datasource="#client.company_dsn#">
	SELECT LK.DESCRIPTION, SD.APPROVED
    FROM CINLEAVEAPPSD SD
    
    LEFT JOIN CLKLVCODE LK ON LK.LEAVETYPE = SD.LEAVETYPE
    
    WHERE REFERENCEDATE = <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
    AND PERSONNELIDNO = '#appno#'
</cfquery>

<cfif not getLVDtls.RecordCount>
	<cfquery name="getLVMaster" datasource="#client.company_dsn#">
        SELECT LK.DESCRIPTION, SI.APPROVED
        FROM CINLEAVEAPPSI SI

        LEFT JOIN CLKLVCODE LK ON LK.LEAVETYPE = SI.LEAVETYPE
        
        WHERE STARTINGDATE <=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
		AND ENDINGDATE >=  <cfqueryparam cfsqltype="cf_sql_date" value="#sFrom#">
        AND PERSONNELIDNO = '#appno#'
    </cfquery>
    
 	<cfif getLVMaster.RecordCount>
    	<cfset lApprove =  getLVMaster.APPROVED>
        <cfset lDescript =  getLVMaster.DESCRIPTION>
    <cfelse>
    	<cfset lApprove =  ''>
        <cfset lDescript =  ''>
    </cfif>
    
<cfelse>
	<cfset lApprove =  getLVDtls.APPROVED>
    <cfset lDescript =  getLVDtls.DESCRIPTION>
</cfif>


<cfif lApprove neq '' and lDescript neq ''>
	
    <cfset sLeave = "#lDescript#<br />">
    
	<cfif UCASE(lApprove) EQ 'Y'>
    	<cfset sLeave = sLeave&"(APPROVED)">
    <cfelseif UCASE(lApprove) EQ 'N'>
        <cfset sLeave = sLeave&"(DISAPPROVED)">	
    <cfelseif UCASE(lApprove) EQ 'S'>
    	<cfset sLeave = sLeave&"(PENDING)">	
    <cfelseif UCASE(lApprove) EQ 'B'>
	    <cfset sLeave = sLeave&"(RETURNED TO IS)">	
    <cfelseif UCASE(lApprove) EQ 'R'>
    	<cfset sLeave = sLeave&"(RETURNED TO ORIG.)">	
    <cfelse>
    	<cfset sLeave = sLeave&"(NOT YET ROUTED)">	
    </cfif>
    
<cfelse>
   	<cfset sLeave = "">
</cfif>

<CFIF Len(varPATTERNCODE)>
    <cfquery name="getDesc" datasource="#client.company_dsn#">
        SELECT DESCRIPTION FROM CLKWORKSKED
        WHERE WORKSKEDCODE = '#varPATTERNCODE#'
    </cfquery>
    <CFSET varPATTERNCODEDesc=getDesc.DESCRIPTION>
    
    <cfif not getDesc.RecordCount>
    	<cfquery name="getDesc" datasource="#client.company_dsn#">
            SELECT DESCRIPTION FROM CLKWSPATTERN
            WHERE PATTERNCODE = '#varPATTERNCODE#'
        </cfquery>
    	<CFSET varPATTERNCODEDesc=getDesc.DESCRIPTION>
    </cfif>
</CFIF>